package com.bai.ad.service.impl;

import com.bai.ad.constant.CommonStatus;
import com.bai.ad.constant.Constants;
import com.bai.ad.dao.AdPlanRepository;
import com.bai.ad.dao.AdUserRepository;
import com.bai.ad.entity.AdPlan;
import com.bai.ad.entity.AdUser;
import com.bai.ad.service.IAdPlanService;
import com.bai.ad.util.CommonUtils;
import com.bai.ad.vo.AdPlanGetRequest;
import com.bai.ad.vo.AdPlanRequest;
import com.bai.ad.vo.AdPlanResponse;
import com.bao.ad.exception.AdException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * ClassName IAdPlanServiceImpl
 *
 * @author Bai
 * @version 1.0
 * @date 2019/2/19 0019 下午 22:47
 **/
@Slf4j
@Service
public class AdPlanServiceImpl implements IAdPlanService {

    private final AdUserRepository adUserRepository;

    private final AdPlanRepository adPlanRepository;

    public AdPlanServiceImpl(AdUserRepository adUserRepository, AdPlanRepository adPlanRepository) {
        this.adUserRepository = adUserRepository;
        this.adPlanRepository = adPlanRepository;
    }

    /**
     * <h2>创建推广计划</h2>
     */
    @Override
    @Transactional
    public AdPlanResponse createAdPlan(AdPlanRequest request) throws AdException {

        if(!request.createValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        Optional<AdUser> adUser = adUserRepository.findById(request.getId());
        if(!adUser.isPresent()){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }

        AdPlan oldPlan = adPlanRepository.findByUserIdAndPlanName(request.getUserid(), request.getPlanName());
        if(null != oldPlan){
            throw new AdException(Constants.ErrorMsg.SAME_NAME_PLAN_ERROR);
        }

        AdPlan newPlan = adPlanRepository.save(new AdPlan(request.getUserid(), request.getPlanName(),
                CommonUtils.parseStringDate(request.getStartDate()),
                CommonUtils.parseStringDate(request.getEndDate())));

        return new AdPlanResponse(newPlan.getId(), newPlan.getPlanName());
    }

    /**
     * <h2>获取推广计划</h2>
     *
     * @param request
     */
    @Override
    public List<AdPlan> getAdPlanByIds(AdPlanGetRequest request) throws AdException {

        if(!request.validate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        return adPlanRepository.findAllByIdInAndUserId(request.getIds(), request.getUserId());
    }

    /**
     * <h2>更新推广计划</h2>
     *
     * @param request
     */
    @Override
    @Transactional
    public AdPlanResponse updateAdPlan(AdPlanRequest request) throws AdException {

        if(!request.updateValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        AdPlan plan = adPlanRepository.findByIdAndUserId(request.getId(), request.getUserid());
        if(null == plan){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }

        if(null != request.getPlanName()){{
            plan.setPlanName(request.getPlanName());
        }}

        if (null != request.getStartDate()) {
            plan.setStartDate(CommonUtils.parseStringDate(request.getStartDate()));
        }

        if(null != request.getEndDate()){
            plan.setEndDate(CommonUtils.parseStringDate(request.getEndDate()));
        }

        plan.setUpdateTime(new Date());

        adPlanRepository.save(plan);

        return new AdPlanResponse(plan.getId(), plan.getPlanName());
    }

    /**
     * <h2>删除推广计划</h2>
     *
     * @param request
     */
    @Override
    @Transactional(rollbackOn = AdException.class)
    public void deleteAdplan(AdPlanRequest request) throws AdException {

        if(!request.deleteValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        AdPlan plan = adPlanRepository.findByIdAndUserId(request.getId(), request.getUserid());
        if(null == plan){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }

        plan.setPlanStatus(CommonStatus.INVALID.getStatus());
        plan.setUpdateTime(new Date());
        adPlanRepository.save(plan);
    }
}
